package com.jk.libsm.controller;

import com.alibaba.fastjson.JSONObject;
import com.jk.libsm.pojo.User;
import com.jk.libsm.service.UserService;
import com.jk.libsm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.ValidationEvent;

/**
 * @author lk
 * @date 2020/7/6 0006 15:04
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Result userLogin(@RequestBody JSONObject jsonObject) {

        String id = jsonObject.get("id").toString();
        String password = jsonObject.get("password").toString();

        System.out.println(id + password);


        return userService.userLogin(id, password);
    }

    @PostMapping("/regist")
    public Result userRegist(@RequestBody JSONObject jsonObject) {
        String sex;
        String id = jsonObject.get("id").toString();
        String password = jsonObject.get("password").toString();
        String name = jsonObject.get("name").toString();
        String phone_num=jsonObject.get("phone_num").toString();
        if(jsonObject.get("gender").toString().equals('1')){
             sex="男";
        }
        else{
             sex="女";
        }
        String adress=jsonObject.get("adress").toString();
        User user=new User();
        user.setRNo(id);
        user.setReaderPassword(password);
        user.setRName(name);
        user.setReaderPhoneNumber(phone_num);
        user.setRSex(sex);
        user.setReaderAddress(adress);
        System.out.println(id + password+name+sex+adress);
        return userService.userRegister(user);
    }

    @PostMapping("/find_borrow_history")
    public Result userFindBorrowHistory(@RequestBody JSONObject jsonObject){
        String token=jsonObject.get("token").toString();
        return userService.userFindBorrowHistory(token);
    }

    //借书状态查询
    @PostMapping("/find_borrow_state")
    public  Result findBorrowState(@RequestBody JSONObject jsonObject){
        String token = jsonObject.get("token").toString();
        return userService.userFindBorrowState(token);
    }

    //续借操作
    @PostMapping("/delay")
    public Result delay(@RequestBody JSONObject jsonObject){
        String token = jsonObject.get("token").toString();
        int book_id=Integer.parseInt(jsonObject.get("book_id").toString());
        return userService.userDelay(token,book_id);
    }

    //预约操作
    @PostMapping("/order")
    public  Result order(@RequestBody JSONObject jsonObject){
        String token = jsonObject.get("token").toString();
        int book_id=Integer.parseInt(jsonObject.get("book_id").toString());
        return userService.userOrder(token,book_id);
    }

    //预约状态查询
    @PostMapping("/find_order_state")
    public  Result findOrderState(@RequestBody JSONObject jsonObject){
        String token = jsonObject.get("token").toString();
        return  userService.userFindOrderState(token);
    }

     //查询用户详细信息
     @PostMapping("/get_user_info")
     public  Result getUserInfo(@RequestBody JSONObject jsonObject){
         String token = jsonObject.get("token").toString();
         return userService.userGetUserInfo(token);
     }

}
